<template>
    <div>
<!--    <m-header></m-header>-->        
        <m-swiper v-if="datas!=null" :swi="datas.indexImages"></m-swiper>
        <m-nav></m-nav>
        <m-list></m-list>
        <m-guess></m-guess>
        <m-bottom></m-bottom>
    </div>
</template>
<script>
import MHeader from './Header'
import MSwiper from './Swiper'
import MNav from './Nav'
import MList from './List'
import MGuess from './Guess'
import MBottom from './bottom'
export default {
    name: 'index',
    components: {
        MHeader,
        MSwiper,
        MNav,
        MList,
        MGuess,
        MBottom
    },
    data() {
        return {
            datas: null,
            url: 'http://www.jydsapp.com/jydsApi/api/h5/common/getIndexImages',
            openidurl: 'http://www.jydsapp.com/jydsApi/api/h5/common/getWxUserOpenid'
        }
    },
    created() {
        this.getData()
        // this.toGet()
    },
    beforeRouteLeave(to, from, next) {
        if (to.name == 'singlebook') {
            let routeinfo = to.params // 从主页或单词主页到列表页和购买页的数据
            this.$store.commit('saveWordIndexToSingleBookOrBuyData', routeinfo)
            next()
        } else {
            next()
        }
    },
    methods: {
        toGet() {
            let that = this
            if (/micromessenger/i.test(navigator.userAgent)) {
                let code = that.getUrlParam('code')
                if (code == null || code == undefined || code == '') {
                    location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf96319bd0999e9a4&redirect_uri=http%3A%2F%2Fwww.jydsapp.com%2FjydsH5&response_type=code&scope=snsapi_base#wechat_redirect"
                }else{
                    let str = `{"code":"${code}"}`
                    that.fetch({
                        method: 'post',
                        url: that.openidurl,
                        params: {
                            "paramStr": str
                        }
                    }).then((response) => {
                        let openIdData = JSON.parse(response.data.data).openid
                        this.$store.commit('saveOpenIdData', openIdData)  //首页保存openid
                    }).catch((err) => {
                        
                    });
                }
            } else {
                that.$Toast({
                  content: '请在微信内浏览'
                })
            }
        },
        getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg);  //匹配目标参数
            if (r != null) return unescape(r[2]); return null; //返回参数值
        },
        getData() {
            let auth = localStorage.user ? JSON.parse(localStorage.user) : null
            let str = auth ?
                `{"userPhone":"${auth.user.phoneNum}","token":"${auth.token}"}` :
                `{}`
            let toast = this.$Toast({
                content: '加载中…',
                iconClass: 'loader',
                autoClose:false
            })
            this.fetch({
                method: 'post',
                url: this.url,
                params: {
                    "paramStr": str
                }
            }).then((data) => {
                let res = data.data
                if (res.code == 200) {
                    this.datas = JSON.parse(res.data)
                    toast.close()
                } else if (res.code == 103) {  //五天内未登录 token失效跳转到登陆页面
                    toast.close()
                    localStorage.removeItem('user')
                    this.$Toast({
                        content: '请重新登录'
                    })
                    this.$router.replace('/login')
                } else {
                    toast.close()
                    this.$Toast({
                        content: res.message
                    })
                }
            }).catch((err) => {
                this.$Toast({
                    content: err
                })
            })
        }
    }
}
</script>
<style lang="scss" scoped>
</style>
